package com.cd826dong.clouddemo.api;

import com.cd826dong.clouddemo.dto.UserDto;
import com.cd826dong.clouddemo.redis.repository.UserRedisRepository;
import com.cd826dong.clouddemo.service.ProviderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class FeignApi {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ProviderService providerService;

    @Autowired
    protected UserRedisRepository userRedisRepository;

    @RequestMapping(value="/{name}", method = RequestMethod.GET)
    public String hello(@PathVariable String name){

        // 首先从Redis中获取
        String userDto = this.userRedisRepository.findOne(name);
        if (null != userDto) {
            this.logger.debug("已从Redis缓存中获取到用户:{} 的信息", name);
            return userDto;
        }
        // 如果Redis中不存在， 就从远程获取
        this.logger.debug("Redis缓存中不存在用户:{} 的信息， 尝试从远程进行加载",name);

        return providerService.hello(name);
    }

    @RequestMapping(value="/setRedisValue/{name}", method = RequestMethod.GET)
    public void setRedisValue(@PathVariable String name){
        this.userRedisRepository.saveUser(name);
    }
}
